import activityDataService from '../services/activityData.js';
import Result from '../utils/result.js';

const activityDataController = {
  async getTodayActivity(req, res) {
    const { userId } = req.params;

    try {
        const result = await activityDataService.getTodayActivity(userId);

        if (result.code !== 200) {
            return res.statuxs(result.code).json(result.toObject());
        }

        return res.status(200).json(result.toObject());
    } catch (error) {
        return res.status(500).json(Result.error(error.message).toObject());
    }
  },
  async getHistoryActivity(req, res){
      const { userId } = req.params;
      let { startDate, endDate } = req.query;

      if(!startDate || !endDate){
        startDate = new Date(new Date().setDate(new Date().getDate() - 7));
        endDate = new Date();
      }

      try{
          const result = await activityDataService.getHistoryActivity(userId,startDate,endDate);
          if(result.code !== 200){
              return res.status(result.code).json(result.toObject());
          }

          return res.status(200).json(result.toObject());
      }catch(error){
        return res.status(500).json(Result.error(error.message).toObject());
      }

  },
  async getActivityStatistics(req, res){
    const { userId } = req.params;
    const { period } = req.query;

    try{
      const result = await activityDataService.getActivityStatistics(userId, period);

      if(result.code !== 200){
        return res.status(result.code).json(result.toObject());
      }

      return res.status(200).json(result.toObject());
      
      
    }catch(error){
      return res.status(500).json(Result.error(error.message).toObject());
    }
    
  }

};

export default activityDataController;